package com.maidou.qiantai.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.maidou.qiantai.interceptor.LoginAuthInterceptor;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author maidou
 * @Date 2023/3/2 20:23
 */
@Configuration
public class InterceptorWebMvcConfig implements WebMvcConfigurer {

    @Resource
    private LoginAuthInterceptor loginAuthInterceptor;

//    拦截器注册
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(loginAuthInterceptor);
        //拦截对象
        registration.excludePathPatterns("/qiantai/member/login")
                .excludePathPatterns("/qiantai/record/list")
                .excludePathPatterns("/qiantai/spu/image/advertising-list")
                .excludePathPatterns("/qiantai/spu/recommend-list")
                .excludePathPatterns("/qiantai/spu/spuInfo/{spuId}")
                .excludePathPatterns("/qiantai/spu/catalog-list/{catalogId}")
                .excludePathPatterns("/qiantai/spu/comment/list-comment/{spuId}")
                .excludePathPatterns("/qiantai/spu/category/list-tree")
//                .excludePathPatterns("/upload/**")
//                .excludePathPatterns("/admissions/account/login/verify")
                .addPathPatterns("/**");


    }
    //设置图片上传虚拟路径与物理路劲
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        //获取jar包所在目录
//        ApplicationHome h = new ApplicationHome(getClass());
//        File jarF = h.getSource();
//        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
//        String dirPath = jarF.getParentFile().toString()+"/upload/";
//
//        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + dirPath);
//    }

//    @Override
//    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = converter.getObjectMapper();
//        // 生成JSON时,将所有Long转换成String
//        SimpleModule simpleModule = new SimpleModule();
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//        objectMapper.registerModule(simpleModule);
//        // 时间格式化
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//        // 设置格式化内容
//        converter.setObjectMapper(objectMapper);
//        converters.add(0, converter);
//    }
}
